/*
Copyright (c) 2009 Vratislav Cermak (vratacermak@gmail.com)
 
This file is part of TeachMe Project. See http://code.google.com/p/teach-me for more information.
 
TeachMe is an open source project. Using of TeachMe is free of charge.
It is distributed under GNU General Public License version 3 (GPLv3).
GPLv3 can be found at http://www.gnu.org/licenses/gpl.html.
 
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE.  See the license for more details.
*/

using System;
using TeachMe.Application.Data;

namespace TeachMe.Application
{
    [Flags]
    public enum EditedDictionaryValueType
    {
        Enabled = 1,
        Direction = 2,
        Priority = 3
    }

    public class EditedDictionaryDescriptor
    {
        private Dictionary _dictionary;
        private EditedItemState _state;
        private EditedDictionaryValueType _modifiedValues;
        private EntryTextDirection _entryTextDirection;
        private Priority _priority;
        private bool _enabled;

        public static EditedDictionaryDescriptor Create(Dictionary dictionary)
        {
            EditedDictionaryDescriptor result = new EditedDictionaryDescriptor();
                
            result._dictionary = dictionary;
            result._entryTextDirection = dictionary.UserData.EntryTextDirection;
            result._priority = dictionary.UserData.Priority;
            result._enabled = dictionary.UserData.Enabled;
            result._state = EditedItemState.Normal;

            return result;
        }

        public static EditedDictionaryDescriptor CreateNew(Dictionary dictionary)
        {
            EditedDictionaryDescriptor result = new EditedDictionaryDescriptor();

            result._dictionary = dictionary;
            result._entryTextDirection = dictionary.UserData.EntryTextDirection;
            result._priority = dictionary.UserData.Priority;
            result._enabled = dictionary.UserData.Enabled;
            result._state = EditedItemState.New;

            return result;
        }

        internal EditedItemState State
        {
            get { return _state; }
            set { _state = value; }
        }

        public bool IsModified()
        {
            return _modifiedValues != 0;
        }

        public bool IsModified(EditedDictionaryValueType valueType)
        {
            return (_modifiedValues & valueType) == valueType;
        }

        public bool Enabled
        {
            get { return _enabled; }
            set
            {
                if (value != _enabled)
                {
                    _modifiedValues |= EditedDictionaryValueType.Enabled;
                }
                _enabled = value;
            }
        }

        public string Name
        {
            get { return Dictionary.Entries.Name; }
        }

        public string Description
        {
            get { return Dictionary.Entries.Description; }
        }

        public EntryTextDirection EntryTextDirection
        {
            get
            {
                return _entryTextDirection;
            }
            set
            {
                if (_entryTextDirection != value)
                {
                    _modifiedValues |= EditedDictionaryValueType.Direction;
                }
                _entryTextDirection = value;
            }
        }

        public Priority Priority
        {
            get
            {
                return _priority;
            }
            set
            {
                if (_priority != value)
                {
                    _modifiedValues |= EditedDictionaryValueType.Priority;
                }
                _priority = value;
            }
        }

        public Dictionary Dictionary
        {
            get { return _dictionary; }
        }

        public int Count
        {
            get { return _dictionary.Entries.Count; }
        }
    }
}
